using UnityEngine;
using System.Collections;

public class orbit : AIBase
{
	
	public Transform center;
	public int orbitDirection  = 1;
	private Vector3 axis = new Vector3 (0,0,-1);
	public float radius = 2.0f;
	public float radiusSpeed = 0.5f;
	public float rotationSpeed = 80.0f;
    public float centerVerticalSpeed = 1.5f;
 	
	private float mTimer = 0.0f;
	
	public bool isBlackhole = false;
	public float suckRate = .01f;
	
	public GameObject explosion1;
	public override void Die()
	{
		GameObject burst = (GameObject) Instantiate( explosion1,transform.position,Quaternion.identity);
		Destroy(burst,1f);
		print ("I get here");
		Destroy(transform.parent.gameObject);
	}
	
	protected override void Start ()
	{
		base.Start();
		//transform.position = (transform.position - center.position).normalized * radius + center.position;
	}
	protected override void Update ()
	{
       center.position +=  Vector3.down * centerVerticalSpeed;
		transform.RotateAround (center.position, axis*orbitDirection, rotationSpeed * Time.deltaTime);
		var desiredPosition = (transform.position - center.position).normalized * radius + center.position;
		transform.position = Vector3.MoveTowards (transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
		//transform.rotation = Quaternion.Euler(new Vector3(270,0,0));
		//transform.rotation = Quaternion.Euler(Vector3.Cross(new Vector3(transform.rotation.x,transform.rotation.y,transform.rotation.z),Vector3.zero));
		Attack();
		FireTimer += Time.deltaTime;
	 	findAngle();
		if(isBlackhole)
		{
			orbitDirection = 1;
			mTimer += Time.deltaTime;
			if(radius < 0)
			{
				Destroy(gameObject);
			}
			if (mTimer > 2)
			{
				radius -= suckRate;
			}
		}
	}

	float findAngle ()
	{
		float angle = Mathf.Atan2 (center.position.y - transform.position.y, center.position.x - transform.position.x) * 180 / Mathf.PI;
//		Debug.Log(angle);
		return angle;
		
	}
	public void OnCollisionEnter(Collision col)
    {
		if(col.gameObject.tag == "LevelBounds")
		{
			orbitDirection*=-1;
		}
	}

}




